package cc.linker.steplink.crm.task;

import cc.linker.steplink.crm.commons.constant.ContentConstant;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.service.IAwakenService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章标签同步
 */
@Component
@Configuration
public class ArticleLabelTimerTask {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private HttpService httpService;
    @Autowired
    private SystemProperties systemProperties;
    @Autowired
    private IAwakenService iAwakenService;

    @Value("${system.scrm.sync.contentUrl}")
    private String awakenarticleurl;

    @Value("${system.scrm.defimg}")
    private String defimg;

    @Value("${system.scrm.awakenlabelurl}"+"/service/GetLabelList")
    private String syncUrl;

    @RedisLock("ArticleLabelTimerTask")
    @Scheduled(cron = "15 */1 * * * *")
    public void awakenArticleSync() throws Exception {
        String key = "sync_awakenarticle_list";
        String str = stringRedisTemplate.opsForList().leftPop(key);
        if (null == str) {
            return;
        }
        Map map = (Map) JSON.parse(str);
        String classId = (String) map.get("classId");
        String labels = (String) map.get("labels");
        String orgId = (String) map.get("orgId");
        String taskId = (String) map.get("taskId");
        iAwakenService.delAwakenArticle(taskId);

        Map<String, Object> req = new HashMap<>();

        if (!StringUtils.isEmpty(classId)) {
            req.put("classificationId", classId);
        }
        if (!StringUtils.isEmpty(labels)) {
            req.put("labelName", labels);
        }
        if (!StringUtils.isEmpty(orgId)) {
            req.put("organizationNum", orgId);
        }

        String url = systemProperties.getSync().getContentUrl() + ContentConstant.NEWS_BY_STEP;
        String res = httpService.doPost(url, req, ContentConstant.HEADER);
        Map data = (Map) JSON.parse(res);
        if (null == data) {
            return;
        }
        if (!"200".equals(data.get("code"))) {
            return;
        }
        List<Map<String, Object>> resdata = (List) data.get("data");
        for (Map<String, Object> datum : resdata) {
            datum.put("taskId", taskId);
            String img = StringUtils.isEmpty(datum.get("newsCoverFirst")) ? defimg : datum.get("newsCoverFirst").toString();
            String des = StringUtils.isEmpty(datum.get("newsSummary")) ? datum.get("newsName").toString() : datum.get("newsSummary").toString();
            datum.put("newsCoverFirst", img);
            datum.put("newsSummary", des);
            iAwakenService.addAwakenArticle(datum);
        }
    }

}
